package logic

import (
	"context"
	{{.TableCamelNameLower}}Api "mldong/api/{{.TableCamelNameLower}}"
	dao "mldong/internal/modules/{{.ModuleName}}/dao"
	"mldong/internal/modules/{{.ModuleName}}/entity"
	service "mldong/internal/modules/{{.ModuleName}}/service"
	"mldong/utility"
	
	"github.com/gogf/gf/v2/database/gdb"
	"github.com/gogf/gf/v2/frame/g"
	"github.com/jinzhu/copier"
)

func init() {
	// 注册服务实现
	utility.Register(service.{{.TableCamelName}}ServiceType, New{{.TableCamelName}}Logic())
}

type {{.TableCamelName}}Logic struct {
	{{.TableCamelNameLower}}Dao *dao.{{.TableCamelName}}Dao
}

// 保存{{.Remark}}接口实现
func (logic *{{.TableCamelName}}Logic) Save(ctx context.Context, req *{{.TableCamelNameLower}}Api.SaveReq) (res bool, err error) {
	var {{.TableCamelNameLower}} = entity.{{.TableCamelName}}{}
	err = g.DB().Transaction(ctx, func(ctx context.Context, tx gdb.TX) error {
		req.ID = nil
		// 将req对象赋值给{{.TableCamelNameLower}}
		copier.Copy(&{{.TableCamelNameLower}}, &req)
		// 插入{{.ModuleName}}_{{.TableCamelNameLower}}表
		{{.TableCamelNameLower}}, err = logic.{{.TableCamelNameLower}}Dao.Insert({{.TableCamelNameLower}}, tx)
		if err != nil {
			return err
		}
		return nil
	})
	return {{.TableCamelNameLower}}.ID != nil, err
}

// 修改{{.Remark}}接口实现
func (logic *{{.TableCamelName}}Logic) Update(ctx context.Context, req *{{.TableCamelNameLower}}Api.UpdateReq) (res bool, err error) {
	var {{.TableCamelNameLower}} = entity.{{.TableCamelName}}{}
	var count int64
	err = g.DB().Transaction(ctx, func(ctx context.Context, tx gdb.TX) error {
		// 将req对象赋值给{{.TableCamelNameLower}}
		copier.Copy(&{{.TableCamelNameLower}}, &req)
		count, err = logic.{{.TableCamelNameLower}}Dao.UpdateById({{.TableCamelNameLower}}, tx)
		return err
	})
	return count > 0, err
}

// 删除{{.Remark}}接口实现
func (logic *{{.TableCamelName}}Logic) Remove(ctx context.Context, ids []string) (res bool, _ error) {
	// 遍历ids
	// for _, id := range ids {
	// 	logic.{{.TableCamelNameLower}}Dao.DeleteById(id)
	// }
	var count, err = logic.{{.TableCamelNameLower}}Dao.DeleteByIds(ids)
	if err != nil {
		return false, err
	}
	if count == 0 {
		return false, err
	}
	return true, err
}

// 根据ID获取{{.Remark}}信息
func (logic *{{.TableCamelName}}Logic) Detail(ctx context.Context, id string) (res {{.TableCamelNameLower}}Api.{{.TableCamelName}}VO, err error) {
	var {{.TableCamelNameLower}}VO = {{.TableCamelNameLower}}Api.{{.TableCamelName}}VO{}
	var {{.TableCamelNameLower}} entity.{{.TableCamelName}}
	{{.TableCamelNameLower}}, err = logic.{{.TableCamelNameLower}}Dao.GetById(id)

	// 将{{.TableCamelNameLower}}对象赋值给{{.TableCamelNameLower}}VO
	copier.Copy(&{{.TableCamelNameLower}}VO, &{{.TableCamelNameLower}})

	return {{.TableCamelNameLower}}VO, err
}

// 分页查询{{.Remark}}接口实现
func (logic *{{.TableCamelName}}Logic) Page(ctx context.Context, req *{{.TableCamelNameLower}}Api.PageReq) (res {{.TableCamelNameLower}}Api.{{.TableCamelName}}PageVO, err error) {
	// 处理分页参数nil值
	utility.HandlePageParam(&req.PageParam)
	{{.TableCamelNameLower}}s, total, err := logic.{{.TableCamelNameLower}}Dao.Page(*req.PageNum, *req.PageSize, func(model *gdb.Model) *gdb.Model {
		model = utility.BuildQuery(req.PageParam, model){{if .HasSort}}
		if(req.OrderBy == ""){
			model = model.OrderAsc("t.sort")
		}{{end}}
		return model
	})
	var {{.TableCamelNameLower}}VOS = []{{.TableCamelNameLower}}Api.{{.TableCamelName}}VO{}
	copier.Copy(&{{.TableCamelNameLower}}VOS, &{{.TableCamelNameLower}}s)
	{{.TableCamelNameLower}}Page := {{.TableCamelNameLower}}Api.{{.TableCamelName}}PageVO{
		Rows: {{.TableCamelNameLower}}VOS,
	}
	{{.TableCamelNameLower}}Page.PageNum = *req.PageNum
	{{.TableCamelNameLower}}Page.PageSize = *req.PageSize
	{{.TableCamelNameLower}}Page.RecordCount = total
	// 计算 TotalPage
	if *req.PageSize > 0 {
		{{.TableCamelNameLower}}Page.TotalPage = (total + *req.PageSize - 1) / *req.PageSize
	} else {
		{{.TableCamelNameLower}}Page.TotalPage = 0 // 如果 PageSize 为 0，则 TotalPage 为 0
	}
	return {{.TableCamelNameLower}}Page, err
}{{ if .IsTree }}
// 查询{{.Remark}}列表接口实现
func (logic *{{.TableCamelName}}Logic) List(ctx context.Context, req *{{.TableCamelNameLower}}Api.ListReq) (res []*{{.TableCamelNameLower}}Api.{{.TableCamelName}}VO, err error) {
	var {{.TableCamelNameLower}}VOS = []*{{.TableCamelNameLower}}Api.{{.TableCamelName}}VO{}
	{{.TableCamelNameLower}}s, err := logic.{{.TableCamelNameLower}}Dao.List(func(model *gdb.Model) *gdb.Model {
		model = utility.BuildQuery(req.PageParam, model){{if .HasSort}}
		if req.OrderBy == "" {
			model = model.OrderAsc("t.sort")
		}{{ end }}
		return model
	})
	copier.Copy(&{{.TableCamelNameLower}}VOS, &{{.TableCamelNameLower}}s)
	if err != nil {
		return nil, err
	}
	return {{.TableCamelNameLower}}VOS, err
}
// 查询{{.Remark}}树接口实现
func (logic *{{.TableCamelName}}Logic) Tree(ctx context.Context, req *{{.TableCamelNameLower}}Api.TreeReq) (res []*{{.TableCamelNameLower}}Api.{{.TableCamelName}}VO, err error) {
	listReq := {{.TableCamelNameLower}}Api.ListReq{}
	copier.Copy(&listReq, &req)
	res, err = logic.List(ctx, &listReq)
	if err != nil {
		return nil, err
	}

	// 使用泛型 TreeTool
	treeTool := &utility.TreeTool[*{{.TableCamelNameLower}}Api.{{.TableCamelName}}VO]{}
	treeNodes := treeTool.ListToTree(res, "0")

	return treeNodes, nil
}{{ end }}
func New{{.TableCamelName}}Logic() service.I{{.TableCamelName}}Service {
	return &{{.TableCamelName}}Logic{
		{{.TableCamelNameLower}}Dao: dao.New{{.TableCamelName}}Dao(),
	}
}
